﻿/*
 * This is our abstract game state.
 * the abstract gamestate contains the general methods that all gamestates inherit.
 * it implements the a gameState interface
 */

using System;
using System.Collections;
using System.Collections.Generic;
using AiAsteriods.Game.Entities.Agent;
using AiAsteriods.Game.Entities.Base;
using AiAsteriods.Game.Tools;

namespace AiAsteriods.Game.State {
  abstract class GameStateAbstract : GameStateInterface, ICloneable {
    protected ResourceManager rm;
    protected GameStart gs;
    protected Player player;
    protected bool activeState = false;
    protected ArrayList entities;
    protected ArrayList entitiesToAdd;
    protected ArrayList entitiesToRemove;
    protected int totalStateTime = 0;

    public ArrayList Entities { get { return entities; } }
    public Player Player1 { get { return player; } }
    public GameStart GS { get { return gs; } }
    public ResourceManager RM { get { return rm; } }

    public GameStateAbstract() {
    }

    public GameStateAbstract(ResourceManager rm, GameStart gs) {
      this.rm = rm;
      this.gs = gs;

      entities = new ArrayList();
      entitiesToAdd = new ArrayList();
      entitiesToRemove = new ArrayList();
    }

    /*
     * this gets called by gamestart when we should load this states resources.
     */
    public virtual void InitState() {
      throw new NotImplementedException();
    }

    /*
     * this gets called by gamestart when we enter this state.
     */
    public virtual void EnterState(GameStateAbstract previousState) {
      activeState = true;
    }

    /*
     * Used to update all our entities.
     * Every gamestate has an update method that goes through all its entities and updates them.
     * Gets called at least every 10ms
     */
    public virtual void UpdateState(int delta) {
      totalStateTime += delta;
      // remove and adds the entities we decided needing that to be done to them last round
      foreach (EntityAbstract e in entitiesToRemove) { entities.Remove(e); }

      entities.AddRange(entitiesToAdd);
      entitiesToRemove.Clear();
      entitiesToAdd.Clear();

      for (int i = 0; i < entities.Count; i++) {
        ((EntityAbstract)entities[i]).Update(delta);
      }
    }

    public virtual void DrawState() {
      for (int i = 0; i < entities.Count; i++) {
        ((EntityAbstract)entities[i]).Draw();
      }
    }

    public virtual void LeaveState() {
      activeState = false;
    }

    public void AddEntity(EntityAbstract e) {
      entitiesToAdd.Add(e);
    }

    public void RemoveEntity(EntityAbstract e) {
      entitiesToRemove.Add(e);
    }

    public virtual void AddScoreForTime(int delta) {

    }

    public virtual void AddScoreForKill() {

    }

    public virtual object Clone() {
      return null;
    }
  }
}
